// pages/contact/person.js
const nt_util = require('../../utils/nt_util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopBuyer:[],
    btn_saveinvitationCode: false,
    otherInvitationCode:'',
    showAddressList: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this;
      this.userinfo()
  },
 userinfo:function(){
   var that = this;
   let openId = nt_util.getOpenId();
   nt_util.httpRequest('api/life/v1/userinfo', 'post', {
     openid: openId
   }, function (res) {
     that.setData({
       shopBuyer: res.data.shopBuyer
     });
   });
 },
 saveinvitationCode:function(){
     var that = this;
     if (!that.checkParams()) {
       return false;
     }
     if (that.data.btn_saveinvitationCode) {
       wx.showModal({
         title: '提示',
         content: '您已提交！',
         showCancel: false,
         success: function (res) {
           if (res.confirm) {
             console.log('用户点击确定')
           } else if (res.cancel) {
             console.log('用户点击取消')
           }
         }
       })
       return false;
     }
     that.setData({
       btn_saveinvitationCode: true,
     });
     let openId = nt_util.getOpenId();
         var params = {
           openid: openId,
           invitationCode: that.data.otherInvitationCode,
         };
         nt_util.httpRequest('api/life/v1/saveInvitationCode', 'post', params, function (res) {
           if (res.code == 100){
           wx.showToast({
             title: '提交成功',
             icon: 'success',
             duration: 1500,
             complete: function (res) {
               setTimeout(function () {
                //  wx.setStorage({
                //    key: "home_reload",
                //    data: "true"
                //  })
                 wx.switchTab({
                   url: '/pages/contact/person'
                 })
               }, 1500);
             }
           });
           that.setData({
             otherInvitationCode: '',
           })
           } else {
             that.setData({
               btn_saveinvitationCode: false,
             })
             wx.showToast({
               title: '已经填写过邀请码',
               icon: 'none',
               duration: 1500,
               complete: function (res) {
                 setTimeout(function () {
                   //  wx.setStorage({
                   //    key: "home_reload",
                   //    data: "true"
                   //  })
                   wx.switchTab({
                     url: '/pages/contact/person'
                   })
                 }, 1500);
               }
             });
           }

       })
 },
 checkParams: function () {
   var that = this;

   if (!that.data.otherInvitationCode) {
     wx.showToast({
       title: '验证码不能为空',
     })
     return false;
   }
   return true;
 },
 bindKeyInput: function (e) {
   var that = this;
   var dataset = e.currentTarget.dataset;
   var data_type = e.detail.value;
   switch (dataset.type) {
     case 'otherInvitationCode':
       that.setData({
         otherInvitationCode: data_type
       });
       break;
   }
 },
 showTap: function () {
   this.setData({
     showAddressList: true
   });
 },
 hideTap: function () {
   this.setData({
     showAddressList: false
   });
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})